package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public void saveOrUpdate(StrategyCatalog entity) {
        if(entity.getSequence() == null){
            Integer sequence = strategyCatalogMapper
                    .selectSequenceByStrategyId(entity.getStrategyId());

            if(sequence == null){
                sequence = 0;
            }
            entity.setSequence(sequence + 1);
        }
        if(entity.getId()!=null){
            strategyCatalogMapper.updateByPrimaryKey(entity);
        }else {
            strategyCatalogMapper.insert(entity);
        }
    }

    @Override
    public List<StrategyCatalog> selectByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectByStrategyId(strategyId);
    }


    @Override
    public PageInfo<StrategyCatalog> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}
